<template>
    <a-modal
            title="编辑"
            :visible="loading"
            @ok="handleOk"
            @cancel="handleCancel"

    >
        <!--destroyOnClose="true"-->
            <a-form-model :model="form" ref="editTemp" :label-col="labelCol" :wrapper-col="wrapperCol" :rules="rules" >
                <a-form-model-item label="模板名称" prop="templateName">
                    <a-input v-model="form.templateName" />
                </a-form-model-item>         
                <a-form-model-item label="模板正则" prop="templateRegx">
                    <a-input v-model="form.templateRegx" />
                </a-form-model-item>
            </a-form-model>
    </a-modal>
</template>
<script>
    import {getOne,save} from '@/api/template'
    import AInputSearch from 'ant-design-vue/es/input/Search'

    export default {
            name : 'edit',
      components: { AInputSearch },
      data() {
         let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入必填值'));
      } else {
        if (this.form.checkPass !== '') {
          this.$refs.editTemp.validateField('checkPass');
        }
        callback();
      }
    };
              return {
                loading: false,
                labelCol: { span: 4 },
                wrapperCol: { span: 14 },
                form: {
                  id: '',
                  templateName: '',
                  templateRegx: '',
                },
                rules: {
        templateName: [{ required: true, validator: validatePass, trigger: 'change' }],
        templateRegx: [{ required: true, validator: validatePass, trigger: 'change' }]
      },
              };
            },
            methods: {
              getOne(id){
                getOne(id).then(res => {
                  this.form = res.data
                })
              },
              handleOk(){
const _this = this;
                _this.$refs['editTemp'].validate(valid => {
            if (valid) {
                 save(this.form).then(res => {
                  this.$message.success(res.message)
                  this.loading = false
                  this.clearForm()
                  this.$parent.initData();
                })
                } else {
          console.log('error submit!!');
        }
        });
              },
              handleCancel(){
                this.loading = false
                this.clearForm()
              },
              clearForm(){
                Object.keys(this.form).forEach(key => (this.form[key] = ''));
                // this.$refs['editTemp'].resetFields()
              }
            },
          };
        </script>

